package com.lemon.stock.compose

import android.content.Context
import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.KeyboardArrowRight
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.tooling.preview.PreviewParameter
import androidx.compose.ui.tooling.preview.PreviewParameterProvider
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.lemon.stock.R
import com.lemon.stock.local.DestinationType
import com.lemon.stock.local.FuncItem
import com.lemon.stock.local.MineFuncList
//import com.lemon.stock.local.MineFuncList
import com.lemon.stock.theme.*
import com.lemon.stock.viewmodel.MineViewModel

//@Preview
@Composable
fun MineScreen(navController: NavHostController){
    val mViewModel: MineViewModel = viewModel()
    Column(
        modifier = Modifier
            .fillMaxSize()
            .background(color = back001),
        horizontalAlignment = Alignment.CenterHorizontally,

    ) {
        UserPad(navController, mViewModel)
        FuncList()
    }

}

@Composable
fun UserPad(navController: NavHostController, mViewModel: MineViewModel){
    Spacer(modifier = Modifier.height(30.dp))

    Image(
        painter = painterResource(id = R.drawable.ic_portrait),
        contentDescription = "",
        modifier = Modifier
            .width(100.dp)
            .height(100.dp)
            .clickable {
                if (mViewModel.user == null) {
                    navController.navigate(DestinationType.LOGIN)
                }
            }
    )
    if (mViewModel.user == null) {
        Text(text = "未登录")
    }else{
        Text(text = mViewModel.user!!.username)
    }

    Spacer(modifier = Modifier.height(30.dp))
}

@Composable
fun FuncList(){
    val context = LocalContext.current
    LazyColumn(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .background(color = Color.White)
    ) {
        MineFuncList.functions.forEach{ func->
            item{
                MineFuncItem(func = func, context = context)

                Divider(color = back002, thickness = 0.5.dp)
            }
        }

        item{
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(150.dp)
                    .padding(8.dp)
                    .background(Color.White),
                verticalAlignment = Alignment.Top,
                horizontalArrangement = Arrangement.Center
            ) {
                Button(
                    modifier=Modifier.background(Color.LightGray).width(200.dp),
                    colors = ButtonDefaults.buttonColors(
                        backgroundColor = Color.LightGray,
                        contentColor = Color.DarkGray
                    ),
                    onClick = { /*TODO*/ }
                ) {
                    Text(text = "退出登录")
                }
            }
        }

    }
}

@Composable
fun MineFuncItem(func: FuncItem, context: Context){
    Row(
        modifier = Modifier
            .fillMaxWidth()
            .padding(horizontal = 8.dp)
            .height(50.dp)
            .clickable { func.onclick(context) },
        verticalAlignment = Alignment.CenterVertically
    ){
        Image(
            painter = painterResource(id = func.icon),
            contentDescription = "",
            modifier = Modifier.size(25.dp)
        )
        Text(
            text = stringResource(func.title),
            modifier = Modifier.padding(start = 8.dp),
            style = TextStyle(color = Color.Black, fontSize = 16.sp)
        )
        Text(
            text = func.caption,
            modifier = Modifier.padding(start = 8.dp),
            style = TextStyle(color = Color.Gray, fontSize = 12.sp)
        )
        Row(
            horizontalArrangement = Arrangement.End,
            verticalAlignment = Alignment.CenterVertically, modifier = Modifier.fillMaxSize()
        ) {
            Text(
                text = func.direct,
                style = TextStyle(color = Color.LightGray, fontSize = 14.sp),
                modifier = Modifier.padding(start = 16.dp)
            )
            Icon(
                painter = painterResource(id = R.drawable.ic_arrow_right),
                contentDescription = "",
                modifier = Modifier
                    .size(20.dp)
                    .align(Alignment.CenterVertically),
                //tint = text_h2
            )
        }
    }
}
